package top.zhangjianyong.tools.service.api.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.TransactionType;
import top.zhangjianyong.tools.service.api.TransactionTypeService;
import top.zhangjianyong.tools.service.mybatis.ITransactionTypeService;
import top.zhangjianyong.tools.utils.ApiResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ： zhangjianyong
 * @date ： 2022/4/25 上午9:38
 */
@Service
public class TransactionTypeServiceImpl implements TransactionTypeService {

    @Autowired
    private ITransactionTypeService iTransactionTypeService;

    @Override
    public ApiResponse list() {

        List<TransactionType> list = iTransactionTypeService.lambdaQuery()
                .eq(TransactionType::getDelFlag, 0)
                .list();
        Map<String, List<TransactionType>> map = new HashMap<>();
        list.forEach(transactionType -> {
            String type = transactionType.getType();
            if (map.containsKey(type)) {
                List<TransactionType> transactionTypes = map.get(type);
                transactionTypes.add(transactionType);
            } else {
                List<TransactionType> transactionTypes = new ArrayList<>();
                transactionTypes.add(transactionType);
                map.put(type, transactionTypes);
            }
        });

        return ApiResponse.success(map);
    }
}
